package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.comm.LwClientHelper;
import cn.com.taiji.sdk.model.comm.protocol.cdts.passTransaction.CdtsReversalUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.cdts.passTransaction.CdtsReversalUploadResponse;
import cn.com.taiji.sdk.model.comm.protocol.sdts.transaction.ReimburseUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.transaction.ReimburseUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.common.utils.GUID;
import io.renren.modules.basedata.dao.OperlogDao;
import io.renren.modules.basedata.dao.ReimburseUploadDao;
import io.renren.modules.basedata.dao.ReversalUploadDao;
import io.renren.modules.basedata.entity.tbl_Operlog;
import io.renren.modules.basedata.entity.tbl_Reimburse;
import io.renren.modules.basedata.entity.tbl_Reversal;
import io.renren.modules.basedata.service.ReimburseUploadService;
import io.renren.modules.basedata.service.ReversalUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ReimburseUploadServiceImpl implements ReimburseUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    ReimburseUploadDao reimburseUploadDao;
    @Autowired
    OperlogDao operlogDao;


    @Override
    public String reimburseUpload() throws Exception {


        log.info("开始获取退款信息");

        List<tbl_Reimburse> list = null;

        tbl_Reimburse reimburse = null;

        list = getReimburseList();//获取退款信息

        tbl_Operlog operlog = new tbl_Operlog();

        int operResult=0;

        String fileName = "";

        if(list!=null&&list.size()>0){

            for(int i=0;i<list.size();i++){
                //
                reimburse = list.get(i);
                ReimburseUploadRequest request =new ReimburseUploadRequest();
                request.setId(reimburse.getId().trim());
                request.setCardId(reimburse.getCardId().trim());
                request.setUserId(reimburse.getUserID().trim());
                request.setFee(Long.valueOf(reimburse.getFee()));

                fileName = ETCCommHelper.getFileName(request,"65");
                //生成就json文件
//				CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);

                try{
                    ReimburseUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, ReimburseUploadResponse.class);
                    log.info("退款信息返回结果："+response.getInfo());
                    if(response!=null&&response.getInfo().equals("成功！")){

                        //上传成功,更新退款信息表状态

                        if(reimburseUploadDao.updateReimburse(reimburse.getId())==1){
                            log.info("更新退款数据成功");
                            operResult = 1;
                            operlog = getOperlog(38,1,reimburse.getId(),fileName ,operResult, response.getInfo(),"上传成功,更新成功");
                        }else{
                            log.info("更新退款数据失败");
                            operlog = getOperlog(38,1,reimburse.getId(),fileName ,operResult, response.getInfo(),"上传成功,更新失败");
                            operlogDao.insertlog(operlog);
                            //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                        }

//					sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    }else{
                        log.info("上传退款信息失败,失败原因："+response.toJson());
                        reimburseUploadDao.updateReimburseError(reimburse.getId());
                        //sqlMapClient.update("reimburse_ORACLE.updateReimburseError",reimburse.getId());

                        //getOperlog（操作类型,传输类型,发送文件名,接收文件名,操作结果,部中心返回结果,说明）
                        operlog = getOperlog(38,1,reimburse.getId(),fileName ,0, response.getInfo(),response.toJson());
                        operlogDao.insertlog(operlog);
                        //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    }
                }catch(IOException e){
                    log.error("上传退款信息失败,失败原因："+e.getMessage());

//				sqlMapClient.update("reimburse_ORACLE.updateReimburseError",reimburse.getId());

                    operlog = getOperlog(38,1,reimburse.getId(),fileName ,0, "IO异常","上传退款信息失败,系统IO异常:"+e.getMessage());
                    operlogDao.insertlog(operlog);
                    //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    e.printStackTrace();
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传退款信息失败,失败原因："+ apie.getMessage());

                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        if(apie.getErrCode()==707){
                            reimburseUploadDao.updateReimburse(reimburse.getId());
                           // sqlMapClient.update("reimburse_ORACLE.updateReimburse",reimburse.getId());
                        }else{
                            reimburseUploadDao.updateReimburseError(reimburse.getId());
                            //sqlMapClient.update("reimburse_ORACLE.updateReimburseError",reimburse.getId());
                        }
                    }

                    operlog = getOperlog(38,1,reimburse.getId(),fileName,0, String.valueOf(apie.getErrCode()),"上传退款信息失败,系统Apie异常:"+apie.getMessage());
                    operlogDao.insertlog(operlog);
                    //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    apie.printStackTrace();
                    continue;
                }
            }
            return "上传退款信息结束!";
        }else{

            return "无退款信息,上传结束...";
        }


    }

    //获取充值信息
    @SuppressWarnings("unchecked")
    public List<tbl_Reimburse> getReimburseList() throws Exception {
        List<tbl_Reimburse> list;
        try {
            list = reimburseUploadDao.getReimburseList();
           // list = sqlMapClient.queryForList("reimburse_ORACLE.getReimburseList");//
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("获取退款信息失败!错误信息：", e1);
            throw new Exception("获取退款信息失败："+e1.getMessage());
        }
        return list;
    }

    //获取operlog信息
    public tbl_Operlog getOperlog(int operType,int softType,String sendFileName,String reFileName,int operResult,String reResult, String content){
        tbl_Operlog operlog = new tbl_Operlog();
        operlog.setWastesn(new GUID().toString());
        operlog.setOperTime(new Date());
        operlog.setOpertype(operType);
        operlog.setSoftType(softType);
        operlog.setSendFileName(sendFileName);
        operlog.setReFileName(reFileName);
        operlog.setOperResult(operResult);
        operlog.setReResult(reResult);
        operlog.setContent(content);
        return operlog;
    }
}
